package com.bjpowernode.oa.web.action;

import com.bjpowernode.oa.util.DBUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DeptDetailServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String deptno=request.getParameter("deptno");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out= response.getWriter();
        Connection con=null;
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        out.println("<!DOCTYPE html>");
        out.println("<html lang='en'> ");
        out.println("<head>      ");
        out.println("    <meta charset='UTF-8'> ");
        out.println("   <title>部门详情</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>部门详情</h1>");
        out.println("<hr/>");
        try {
            con= DBUtil.getConnection();
            String sql="select dname,loc from dept where deptno=?";
            pstmt=con.prepareStatement(sql);
            pstmt.setInt(1,Integer.parseInt(deptno));
            rs=pstmt.executeQuery();
            if(rs.next()){
                String dname=rs.getString("dname");
                String loc=rs.getString("loc");
                out.println("部门编号："+deptno+"<br/>");
                out.println("部门名称："+dname+"<br/>");
                out.println("部门位置："+loc+"<br/>");
            }
        } catch (SQLException exception) {
            exception.printStackTrace();
        }finally {
            DBUtil.close(con,pstmt,rs);
        }
        out.println("<input type='button' value='后退' onclick='window.history.back()'>");
        out.println("</body>");
        out.println("</html>");
    }
}
